<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        
    </style>
</head>
<body>
    
</body>
<script>
    
    class myEvent{
        constructor(){
            // 消息盒子
            this.message = {
            }
        }
        on(type, cb){
            if(this.message[type]){
                this.message[type].push(cb)
            }else{
                this.message[type] = [cb]
            }
        }
        off(type, cb){
            if(!this.message[type]) return;
            for(var i=0;i<this.message[type].length;i++){
                if(this.message[type][i] === cb){
                    this.message[type].splice(i, 1)
                    break;
                }
            }
        }
        emit(type){
            if(!this.message[type]) return;
            for(var i=0;i<this.message[type].length;i++){
                this.message[type][i]()
            }
        }
    }

    var me = new myEvent();


    me.on("a",fn1)
    function fn1(){
        console.log("a事件的事件处理函数1被触发了")
    }

    me.on("a",fn2)
    function fn2(){
        console.log("a事件事件处理函数2被触发了")
    }
    
    me.on("a",fn3)
    function fn3(){
        console.log("a事件事件处理函数3被触发了")
    }

    me.off("a", fn2)

    me.emit("a")

    console.log(me.message)
</script>
</html>